package com.example.mcodebox;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.database.DBOpenHelper;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.LiveFolders;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;

@SuppressLint("NewApi")
public class ShowActivity extends ListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.fragment_show);

		// 实例化数据库帮助类
		final DBOpenHelper helper = new DBOpenHelper(this);
		// 查询获取游标
		Cursor cursor = helper.query();
		String[] from = { "company", "account", "password", "mark" };
		// map存在list中，map中存要输入的内容
		List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
		while (cursor.moveToNext()) {
			// 使用cursor.moveToNext获取表中的数据，以字符串形式获取
			Map<String, String> map = new HashMap<String, String>();
			map.put(from[0], cursor.getString(0));
			map.put(from[1], cursor.getString(1));
			map.put(from[2], cursor.getString(2));
			map.put(from[3], cursor.getString(3));
			lists.add(map);
		}
		// 列表项数组

		// 列表项ID
		int[] to = { R.id.text1, R.id.text2, R.id.text3, R.id.text4 };
		// 适配器
		// @SuppressWarnings("deprecation")
		// SimpleCursorAdapter apt = new SimpleCursorAdapter(this,
		// R.layout.fragment_show, cursor, from, to);
		// SimpleCursorAdapter apt = new SimpleCursorAdapter(this,
		// R.layout.fragment_show, cursor, from, to,
		// SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		SimpleAdapter apt = new SimpleAdapter(this, lists,
				R.layout.fragment_show, from, to);
		// 列表视图
		ListView listView = getListView();
		// 为列表视图添加适配器
		listView.setAdapter(apt);

		// 提示对话框
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		// 为listView添加监听器
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {

				final long temp = id;
				System.out.println("temp的值-----" + temp);
				builder.setMessage("你真的要删除么")
						.setPositiveButton("是的",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// 删除数据
										helper.del((int) temp);
										// 重新查询
										Cursor cursor = helper.query();
										// 列表项数组
										String[] from = { "_id", "company",
												"account", "password", "mark" };
										// 列表项的ID
										int[] to = { R.id.text0, R.id.text1,
												R.id.text2, R.id.text3,
												R.id.text4 };
										// 适配器
										@SuppressWarnings("deprecation")
										// SimpleCursorAdapter apt = new
										// SimpleCursorAdapter(
										// getApplicationContext(),
										// R.layout.fragment_show, cursor,
										// from, to);这个已经过时了，，所以采用下边的
										SimpleCursorAdapter apt = new SimpleCursorAdapter(
												getApplicationContext(),
												R.layout.fragment_show, cursor,
												from, to, which);
										// 列表视图
										ListView listView = getListView();
										// 为列表视图添加适配器
										listView.setAdapter(apt);
									}
								})
						.setNegativeButton("否",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {

									}
								});
				// 创建该dialog
				AlertDialog alertDialog = builder.create();
				alertDialog.show();// 显示该dialog

				return false;
			}
		});

		helper.close();// 关闭该数据库帮助类的实例
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_show, container,
					false);
			return rootView;
		}
	}

}
